package isoft.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductDatabase {
    private Map<String, Product> products;
    private Map<String, User> users;
    private List<Order> orders;

    public ProductDatabase() {
        this.products = new HashMap<>();
        this.users = new HashMap<>();
        this.orders = new ArrayList<>();
    }

    public void addProduct(Product product) {
        products.put(product.getId(), product);
    }

    public boolean removeProductById(String id) {
        return products.remove(id) != null;
    }

    public Product findProductById(String id) {
        return products.get(id);
    }

    public Product findProductByName(String name) {
        for (Product product : products.values()) {
            if (product.getName().equalsIgnoreCase(name)) {
                return product;
            }
        }
        return null;
    }

    public void updateProduct(Product product) {
        products.put(product.getId(), product);
    }

    public void addUser(User user) {
        users.put(user.getUsername(), user);
    }

    public User findUserByUsername(String username) {
        return users.get(username);
    }

    public void updateUser(User user) {
        users.put(user.getUsername(), user);
    }

    public void addOrder(Order order) {
        orders.add(order);
    }

    public List<Order> getAllOrders() {
        return orders;
    }

    public List<Order> getOrdersByUser(User user) {
        List<Order> userOrders = new ArrayList<>();
        for (Order order : orders) {
            if (order.getUser().equals(user)) {
                userOrders.add(order);
            }
        }
        return userOrders;
    }
}
